package src.java.easy;

import java.util.Arrays;

public class Solution500 {
    public static String[] findWords(String[] words) {
        String firstRow = "qwertyuiop";
        String secondRow = "asdfghjkl";
        String thirdRow = "zxcvbnm";
        String[] result = new String[words.length];
        int j = 0;
        for (String word : words) {
            if(word.length() == 1){
                result[j++] = word;
            }
            char c = word.charAt(0);
            if (firstRow.indexOf(Character.toLowerCase(c)) >= 0) {
                for (int i = 1; i < word.length(); i++) {
                    char c2 = word.charAt(i);
                    if (firstRow.indexOf(Character.toLowerCase(c2)) < 0) {
                        break;
                    }
                    if(i == word.length()-1) result[j++] = word;
                }
            } else if (secondRow.indexOf(Character.toLowerCase(c)) >= 0) {
                for (int i = 1; i < word.length(); i++) {
                    char c2 = word.charAt(i);
                    if (secondRow.indexOf(Character.toLowerCase(c2)) < 0) {
                        break;
                    }
                    if(i == word.length()-1) result[j++] = word;
                }
            } else if (thirdRow.indexOf(Character.toLowerCase(c)) >= 0) {
                for (int i = 1; i < word.length(); i++) {
                    char c2 = word.charAt(i);
                    if (thirdRow.indexOf(Character.toLowerCase(c2)) < 0) {
                        break;
                    }
                    if(i == word.length()-1) result[j++] = word;
                }
            }
        }
        result = Arrays.copyOf(result, j);
        return result;
    }

    public static void main(String[] args) {
        String[] words = {"Hello", "Alaska", "Dad", "Peace"};
        findWords(words);
    }
}
